const app = new getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    prompt: '',
    default_img: {},
    goods_list: [],
    info: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let that = this;
    let topic_id = options.id;
    let topic_name = options.name;

    wx.setNavigationBarTitle({
      title: topic_name,
    })
    app.defaultImg(that, true, topic_id);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  loadInfo: function (topic_id) {
    let that = this;
    app.sendRequest({
      url: 'System.Goods.promotionTopicDetail',
      data: {
        topic_id: topic_id
      },
      success: function (res) {
        let code = res.code;
        let data = res.data;
        console.log(res);
        if (code == 0) {
          let info = data;
          let goods_list = info.goods_list;
          delete info.goods_list;

          for (let index in goods_list) {
            if (goods_list[index].picture_info != undefined && goods_list[index].picture_info != '') {
              let img = goods_list[index].picture_info.pic_cover_small;
              goods_list[index].picture_info.pic_cover_small = app.IMG(img);
            }
          }

          info.background_img = app.IMG(info.background_img);
          info.picture_img = app.IMG(info.picture_img);
          info.scroll_img = app.IMG(info.scroll_img);

          for (let index in goods_list) {
            let img = goods_list[index].pic_cover_small;
            goods_list[index].pic_cover_small = app.IMG(img);
          }

          that.setData({
            info: info,
            goods_list: goods_list,
            is_load: 1
          })
        }
      }
    });
  },

  /**
   * 图片加载失败
   */
  errorImg: function(e) {
    let that = this;
    let index = e.currentTarget.dataset.index;
    let goods_list = that.data.goods_list;
    let default_img = that.data.default_img;
    let parm = {};
    let img = goods_list[index].pic_cover_small;

    if (default_img.is_use == 1) {
      default_img = default_img.value.default_goods_img;
      if (img.indexOf(default_img) != 1) {
        let parm_key = "goods_list[" + index + "].picture_info.pic_cover_small";

        parm[parm_key] = default_img;
        that.setData(parm);
      }
    }
  },
})